<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>I/O Flags</title>
  <link href="cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="index.html">cppreference.com</a> &gt; I/O Flags
  </div>

  <h1>C++ I/O Flags</h1>

  <a name="format_flags">
  <h2>Format flags</h2>
  </a>

  <p>C++ defines some format flags for standard input and output, which
  can be manipulated with the <a href="cppio/flags.html">flags()</a>,
  <a href="cppio/setf.html">setf()</a>, and <a href=
  "cppio/unsetf.html">unsetf()</a> functions. For example,</p>
  <pre>
 cout.setf(ios::left); 
</pre>

  <p>turns on left justification for all output directed to
  <strong>cout</strong>.</p>

  <table class="misc-table">
    <tr class="misc-table-tr-1">
      <th>Flag</th>

      <th>Meaning</th>
    </tr>

    <tr class="misc-table-tr-2">
      <td>boolalpha</td>

      <td>Boolean values can be input/output using the words
      &quot;true&quot; and &quot;false&quot;.</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>dec</td>

      <td>Numeric values are displayed in decimal.</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>fixed</td>

      <td>Display floating point values using normal notation (as
      opposed to scientific).</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>hex</td>

      <td>Numeric values are displayed in hexidecimal.</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>internal</td>

      <td>If a numeric value is padded to fill a field, spaces are
      inserted between the sign and base character.</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>left</td>

      <td>Output is left justified.</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>oct</td>

      <td>Numeric values are displayed in octal.</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>right</td>

      <td>Output is right justified.</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>scientific</td>

      <td>Display floating point values using scientific notation.</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>showbase</td>

      <td>Display the base of all numeric values.</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>showpoint</td>

      <td>Display a decimal and extra zeros, even when not needed.</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>showpos</td>

      <td>Display a leading plus sign before positive numeric
      values.</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>skipws</td>

      <td>Discard whitespace characters (spaces, tabs, newlines) when
      reading from a stream.</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>unitbuf</td>

      <td>Flush the buffer after each insertion.</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>uppercase</td>

      <td>Display the &quot;e&quot; of scientific notation and the
      &quot;x&quot; of hexidecimal notation as capital letters.</td>
    </tr>
  </table>

  <a name="manipulators">
  <h2>Manipulators</h2>
  </a>

  <p>You can also manipulate flags indirectly, using the following
  <i>manipulators</i>. Most programmers are familiar with the
  <strong>endl</strong> manipulator, which might give you an idea of
  how manipulators are used. For example, to set the <i>dec</i> flag,
  you might use the following command:</p>
  <pre>
  cout &lt;&lt; dec;
</pre>

  <table class="misc-table">
    <tr class="misc-table-tr-1">
      <th colspan="4">Manipulators defined in &lt;iostream&gt;</th>
    </tr>

    <tr class="misc-table-tr-1">
      <th>Manipulator</th>

      <th>Description</th>

      <th>Input</th>

      <th>Output</th>
    </tr>

    <tr class="misc-table-tr-2">
      <td>boolalpha</td>

      <td>Turns on the boolalpha flag</td>

      <td>X</td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>dec</td>

      <td>Turns on the dec flag</td>

      <td>X</td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>endl</td>

      <td>Output a newline character, flush the stream</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>ends</td>

      <td>Output a null character</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>fixed</td>

      <td>Turns on the fixed flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>flush</td>

      <td>Flushes the stream</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>hex</td>

      <td>Turns on the hex flag</td>

      <td>X</td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>internal</td>

      <td>Turns on the internal flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>left</td>

      <td>Turns on the left flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>noboolalpha</td>

      <td>Turns off the boolalpha flag</td>

      <td>X</td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>noshowbase</td>

      <td>Turns off the showbase flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>noshowpoint</td>

      <td>Turns off the showpoint flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>noshowpos</td>

      <td>Turns off the showpos flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>noskipws</td>

      <td>Turns off the skipws flag</td>

      <td>X</td>

      <td></td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>nounitbuf</td>

      <td>Turns off the unitbuf flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>nouppercase</td>

      <td>Turns off the uppercase flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>oct</td>

      <td>Turns on the oct flag</td>

      <td>X</td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>right</td>

      <td>Turns on the right flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>scientific</td>

      <td>Turns on the scientific flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>showbase</td>

      <td>Turns on the showbase flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>showpoint</td>

      <td>Turns on the showpoint flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>showpos</td>

      <td>Turns on the showpos flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>skipws</td>

      <td>Turns on the skipws flag</td>

      <td>X</td>

      <td></td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>unitbuf</td>

      <td>Turns on the unitbuf flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>uppercase</td>

      <td>Turns on the uppercase flag</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>ws</td>

      <td>Skip any leading whitespace</td>

      <td>X</td>

      <td></td>
    </tr>
  </table>

  <table class="misc-table">
    <tr class="misc-table-tr-1">
      <th colspan="4">Manipulators defined in &lt;iomanip&gt;</th>
    </tr>

    <tr class="misc-table-tr-1">
      <th>Manipulator</th>

      <th>Description</th>

      <th>Input</th>

      <th>Output</th>
    </tr>

    <tr class="misc-table-tr-2">
      <td>resetiosflags( long f )</td>

      <td>Turn off the flags specified by <i>f</i></td>

      <td>X</td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>setbase( int base )</td>

      <td>Sets the number base to <i>base</i></td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>setfill( int ch )</td>

      <td>Sets the fill character to <i>ch</i></td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>setiosflags( long f )</td>

      <td>Turn on the flags specified by <i>f</i></td>

      <td>X</td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>setprecision( int p )</td>

      <td>Sets the number of digits of precision</td>

      <td></td>

      <td>X</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>setw( int w )</td>

      <td>Sets the field width to <i>w</i></td>

      <td></td>

      <td>X</td>
    </tr>
  </table>

  <a name="state_flags">
  <h2>State flags</h2>
  </a>

  <p>The I/O stream state flags tell you the current state of an I/O
  stream. The flags are:</p>

  <table class="misc-table">
    <tbody>
      <tr class="misc-table-tr-1">
        <th>Flag</th>

        <th>Meaning</th>
      </tr>

      <tr class="misc-table-tr-2">
        <td>badbit</td>

        <td>a fatal error has occurred</td>
      </tr>

      <tr class="misc-table-tr-1">
        <td>eofbit</td>

        <td>EOF has been found</td>
      </tr>

      <tr class="misc-table-tr-2">
        <td>failbit</td>

        <td>a nonfatal error has occurred</td>
      </tr>

      <tr class="misc-table-tr-1">
        <td>goodbit</td>

        <td>no errors have occurred</td>
      </tr>
    </tbody>
  </table>

  <a name="mode_flags">
  <h2>Mode flags</h2>
  </a>

  <p>The I/O stream mode flags allow you to access files in different
  ways. The flags are:</p>

  <table class="misc-table">
    <tbody>
      <tr class="misc-table-tr-1">
        <th>Mode</th>

        <th>Meaning</th>
      </tr>

      <tr class="misc-table-tr-2">
        <td>ios::app</td>

        <td>append output</td>
      </tr>

      <tr class="misc-table-tr-1">
        <td>ios::ate</td>

        <td>seek to EOF when opened</td>
      </tr>

      <tr class="misc-table-tr-2">
        <td>ios::binary</td>

        <td>open the file in binary mode</td>
      </tr>

      <tr class="misc-table-tr-1">
        <td>ios::in</td>

        <td>open the file for reading</td>
      </tr>

      <tr class="misc-table-tr-2">
        <td>ios::out</td>

        <td>open the file for writing</td>
      </tr>

      <tr class="misc-table-tr-1">
        <td>ios::trunc</td>

        <td>overwrite the existing file</td>
      </tr>
    </tbody>
  </table>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
